<template>
  <div>
    <el-dialog
      title="新增员工"
      width="50%"
      :visible="isShow"
      @close="closeEvetn"
    >
      <el-form
        ref="form"
        label-width="150px"
        label-position="left"
        :model="form"
        :rules="rules"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker v-model="form.timeOfEntry" placeholder="请选择时间" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请输入">
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div ref="treeArea">
            <el-input
              v-model="form.departmentName"
              readonly
              placeholder="请输入"
              @focus="focusEvent"
            />

            <el-tree
              v-if="showTree"
              :data="trrData"
              :props="{ label: 'name' }"
              @node-click="nodeClick"
            />
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            placeholder="请选择时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="btnfooter">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="closeEvetn">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { companyDepartment } from '@/api/departments.js'
import employees from '@/assets/constant/employees.js'
import { sysUserPost } from '@/api/employess.js'
export default {
  name: 'Dialog',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showTree: false,
      trrData: [],
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        // 姓名验证
        username: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 2,
            max: 12,
            message: '姓名必须为2~12位字符',
            trigger: 'change'
          }
        ],
        // 手机号验证
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const res =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              res.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'change'
          }
        ],
        // 聘用形式
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        // 工号
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        // 组织名称
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        // 入职时间
        timeOfEntry: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (this.form.correctionTime) {
                // 转正时间戳
                const entrytime = this.form.correctionTime.getTime()
                // 入职时间戳
                const valuetime = value.getTime()
                entrytime < valuetime
                  ? callback(new Error('入职时间不能早于转正时间'))
                  : callback()
              }
            },
            trigger: 'change'
          }
        ],
        // 转正时间
        correctionTime: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (this.form.timeOfEntry) {
                // 入职时间戳
                const entrytime = this.form.timeOfEntry.getTime()
                // 转正时间戳
                const valuetime = value.getTime()
                entrytime > valuetime
                  ? callback(new Error('入职时间不能早于转正时间'))
                  : callback()
              }
            },
            trigger: 'change'
          }
        ]
      },
      hireType: employees.hireType
    }
  },
  mounted() {
    document.addEventListener('click', this.clickEvent)
  },
  methods: {
    //  关闭弹窗
    closeEvetn() {
      this.$emit('update:isShow', false)
    },
    async focusEvent() {
      this.showTree = true
      if (this.trrData.length === 0) {
        const res = await companyDepartment()
        this.trrData = this.changefocus(res.data.depts)
        console.log('1111', res)
      }
    },
    // 把拿到的数据转成自己需要的数据方法
    changefocus(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          return (item.children = this.changefocus(arr, item.id))
        }
      })
    },
    // 树形控件节点被点击时的回调
    nodeClick(data) {
      this.form.departmentName = data.name
    },
    clickEvent(e) {
      //   console.log(e.target)
      //   判断点击的位置是不是包含在输入框树形控件外面
      if (this.showTree && !this.$refs.treeArea.contains(e.target)) {
        this.showTree = false
      }
    },
    // 提交按钮
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          const res = await sysUserPost(this.form)
          this.$message.success('新增成功')
          //   刷新父级列表数据
          this.closeEvetn()
          console.log('11111', res)
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.btnfooter {
  text-align: center;
}
</style>
